﻿using System;
using Castle.MonoRail.Framework;
using Hadlow.Jobvertizer.Models;
using Hadlow.Jobvertizer.Repositories;

namespace Hadlow.Jobvertizer.Components
{
    public class Menu : ViewComponent
    {
        ISecurityHelper securityHelper;
        IPosterRepository posterRepository;

        public Menu(
            ISecurityHelper securityHelper, 
            IPosterRepository posterRepository)
        {
            this.securityHelper = securityHelper;
            this.posterRepository = posterRepository;
        }

        public override void Render()
        {
            securityHelper.Context = this.RailsContext;
            User currentUser = securityHelper.CurrentUser;

            if (currentUser.Role == Role.Poster)
            {
                Poster poster = posterRepository.GetPosterFor(currentUser);
                PropertyBag["poster"] = poster;
            }

            base.Render();
        }

        public IRailsEngineContext ContextForRails
        {
            get
            {
                // why isn't this public ????
                return RailsContext;
            }
        }
    }
}
